namespace PickGold.Charting
{
	using System;
	using System.Drawing;
	using System.Security.Permissions;
	using System.Web;

	[SRDescription("DescriptionAttributeLegendCollection_LegendCollection"), AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
	public class LegendCollection : ChartNamedElementCollection<Legend>
	{
		internal LegendCollection(ChartPicture chartPicture)
			: base(chartPicture)
		{
		}

		public Legend Add(string name)
		{
			Legend item = new Legend(name);
			base.Add(item);
			return item;
		}

		internal void CalcInsideLegendPosition(ChartGraphics chartGraph, float elementSpacing)
		{
			if ((base.Common != null) && (base.Common.ChartPicture != null))
			{
				foreach (Legend legend in this)
				{
					if (legend.DockedToChartArea != "NotSet")
					{
						try
						{
							ChartArea local1 = base.Common.ChartPicture.ChartAreas[legend.DockedToChartArea];
							continue;
						}
						catch
						{
							throw new ArgumentException(SR.ExceptionLegendDockedChartAreaIsMissing(legend.DockedToChartArea));
						}
					}
				}
				foreach (ChartArea area in base.Common.ChartPicture.ChartAreas)
				{
					if (area.Visible)
					{
						RectangleF chartAreasRectangle = area.PlotAreaPosition.ToRectangleF();
						float num = Math.Min((float)((chartAreasRectangle.Height / 100f) * elementSpacing), (float)((chartAreasRectangle.Width / 100f) * elementSpacing));
						foreach (Legend legend2 in this)
						{
							if ((legend2.IsEnabled() && legend2.IsDockedInsideChartArea) && ((legend2.DockedToChartArea == area.Name) && legend2.Position.Auto))
							{
								legend2.CalcLegendPosition(chartGraph, ref chartAreasRectangle, num);
							}
						}
						continue;
					}
				}
			}
		}

		internal void CalcLegendPosition(ChartGraphics chartGraph, ref RectangleF chartAreasRectangle, float elementSpacing)
		{
			foreach (Legend legend in this)
			{
				if ((legend.IsEnabled() && (legend.DockedToChartArea == "NotSet")) && legend.Position.Auto)
				{
					legend.CalcLegendPosition(chartGraph, ref chartAreasRectangle, elementSpacing);
				}
			}
		}

		internal void CalcOutsideLegendPosition(ChartGraphics chartGraph, ChartArea area, ref RectangleF chartAreasRectangle, float elementSpacing)
		{
			if ((base.Common != null) && (base.Common.ChartPicture != null))
			{
				float num = Math.Min((float)((chartAreasRectangle.Height / 100f) * elementSpacing), (float)((chartAreasRectangle.Width / 100f) * elementSpacing));
				foreach (Legend legend in this)
				{
					if ((legend.DockedToChartArea != "NotSet") && (base.Chart.ChartAreas.IndexOf(legend.DockedToChartArea) < 0))
					{
						throw new ArgumentException(SR.ExceptionLegendDockedChartAreaIsMissing(legend.DockedToChartArea));
					}
					if ((legend.IsEnabled() && !legend.IsDockedInsideChartArea) && ((legend.DockedToChartArea == area.Name) && legend.Position.Auto))
					{
						legend.CalcLegendPosition(chartGraph, ref chartAreasRectangle, num);
						RectangleF ef = legend.Position.ToRectangleF();
						if (legend.Docking == Docking.Top)
						{
							ef.Y -= num;
							if (!area.Position.Auto)
							{
								ef.Y -= ef.Height;
							}
						}
						else if (legend.Docking == Docking.Bottom)
						{
							ef.Y += num;
							if (!area.Position.Auto)
							{
								ef.Y = area.Position.Bottom + num;
							}
						}
						if (legend.Docking == Docking.Left)
						{
							ef.X -= num;
							if (!area.Position.Auto)
							{
								ef.X -= ef.Width;
							}
						}
						if (legend.Docking == Docking.Right)
						{
							ef.X += num;
							if (!area.Position.Auto)
							{
								ef.X = area.Position.Right + num;
							}
						}
						legend.Position.SetPositionNoAuto(ef.X, ef.Y, ef.Width, ef.Height);
					}
				}
			}
		}

		internal void ChartAreaNameReferenceChanged(object sender, NameReferenceChangedEventArgs e)
		{
			if (e.OldElement != null)
			{
				foreach (Legend legend in this)
				{
					if (legend.DockedToChartArea == e.OldName)
					{
						legend.DockedToChartArea = e.NewName;
					}
				}
			}
		}

		internal string DefaultNameReference
		{
			get
			{
				if (base.Count <= 0)
				{
					return string.Empty;
				}
				return base[0].Name;
			}
		}
	}
}

